package com.csmht.tododemo.handler;


import com.csmht.tododemo.exiception.BaseException;
import com.csmht.tododemo.exiception.ServiceException;
import com.csmht.tododemo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public Result<String> serviceException(ServiceException e) {
        log.error("服务器错误:{}",e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    public Result<String> exceptionHandler(BaseException ex){
        log.error("业务异常信息：{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }

    @ExceptionHandler(SQLException.class)
    public Result<String> sqlExceptionHandler(BaseException ex){
        log.error("SQL异常信息：{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }

}
